package com.dianrui.fmi.activity;


import android.os.Process;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import com.dianrui.fmi.R;
import com.dianrui.fmi.UpdateApps;
import com.dianrui.fmi.event.SwitchAccount;
import com.dianrui.fmi.event.SwitchQuotes;
import com.dianrui.fmi.fragment.AccountFragment;
import com.dianrui.fmi.fragment.CommunityFragment;
import com.dianrui.fmi.fragment.HomeFragment;
import com.dianrui.fmi.fragment.MyFragment;
import com.dianrui.fmi.fragment.QutoesFragment;
import com.dianrui.fmi.util.MyUtil;
import com.dianrui.fmi.util.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

public class MainActivity extends BaseActivity {

    @BindView(R.id.act_main_group_radioGroup)
    RadioGroup actMainGroupRadioGroup;

    //主页
    @BindView(R.id.act_main_radio_home)
    RadioButton actMainRadioHome;
    @BindView(R.id.act_main_rl_radio_home)
    RelativeLayout actMainRlRadioHome;

    //行情
    @BindView(R.id.act_main_quotes_works)
    RadioButton actMainQuotesWorks;
    @BindView(R.id.act_main_rl_quotes_work)
    RelativeLayout actMainRlQuotesWork;

    //社区
    @BindView(R.id.act_main_comunity_publish)
    RadioButton actMainComunityPublish;
    @BindView(R.id.act_main_community_publish)
    RelativeLayout actMainCommunityPublish;

    //开户
    @BindView(R.id.main_account_msg)
    RadioButton mainAccountMsg;
    @BindView(R.id.act_main_rl_account_rl_radio_msg)
    RelativeLayout actMainRlAccountRlRadioMsg;

    //我的
    @BindView(R.id.act_main_my_account)
    RadioButton actMainMyAccount;
    @BindView(R.id.act_main_rl_radio_my)
    RelativeLayout actMainRlRadioMy;

    private RadioButton[] button_radios;

    private int currentShowFragment;
    private int lastShowFragment = 0;

    private long clicktime;

    //主页的fragment
    private HomeFragment homeFragment;

    //行情的fragment
    private QutoesFragment qutoesFragment;

    //社区的fragment
    private CommunityFragment communityFragment;

    //开户的fragment
    private AccountFragment accountFragment;

    //我的fragment
    private MyFragment myFragment;

    @Override
    public int getContentViewId() {
        return R.layout.main;
    }



    @Override
    public void initView() {
        EventBus.getDefault().register(this);
        button_radios = new RadioButton[]{actMainRadioHome, actMainQuotesWorks, actMainComunityPublish, mainAccountMsg, actMainMyAccount};
        changePage(0);
        UpdateApps.checkUpdate(MainActivity.this, false, true);
    }

    //切换到开户的fragment
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(SwitchAccount switchAccount) {
        changePage(3);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(SwitchQuotes switchQuotes) {
        changePage(1);
    }


    private void changePage(int position) {
        currentShowFragment = position;
        if (button_radios != null) {
            button_radios[lastShowFragment].setChecked(false);
        }

        if (button_radios != null) {
            for (int i = 0; i < button_radios.length; i++) {
                button_radios[i].setChecked(false);
            }
        }
        button_radios[position].setChecked(true);

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        hideFragments(transaction);
        switch (position) {
            case 0:
                if (homeFragment == null) {
                    homeFragment = new HomeFragment();
                    transaction.add(R.id.act_main_container, homeFragment);
                } else {
                    transaction.show(homeFragment);
                }
                break;

            case 1:
                if (qutoesFragment == null) {
                    qutoesFragment = new QutoesFragment();
                    transaction.add(R.id.act_main_container, qutoesFragment);
                } else {
                    transaction.show(qutoesFragment);
                }
                break;

            case 2:
                if (communityFragment == null) {
                    communityFragment = new CommunityFragment();
                    transaction.add(R.id.act_main_container, communityFragment);
                } else {
                    transaction.show(communityFragment);
                }
                break;

            case 3:
                if (accountFragment == null) {
                    accountFragment = new AccountFragment();
                    transaction.add(R.id.act_main_container, accountFragment);
                } else {
                    transaction.show(accountFragment);
                }
                break;

            case 4:
                if (myFragment == null) {
                    myFragment = new MyFragment();
                    transaction.add(R.id.act_main_container, myFragment);
                } else {
                    transaction.show(myFragment);
                }
                break;
            default:
                break;
        }
        transaction.commitAllowingStateLoss();
        lastShowFragment = position;
    }

    private void hideFragments(FragmentTransaction transaction) {
        if (homeFragment != null) {
            transaction.hide(homeFragment);
        }
        if (qutoesFragment != null) {
            transaction.hide(qutoesFragment);
        }
        if (communityFragment != null) {
            transaction.hide(communityFragment);
        }
        if (accountFragment != null) {
            transaction.hide(accountFragment);
        }
        if (myFragment != null) {
            transaction.hide(myFragment);
        }
    }

    @OnClick({R.id.act_main_radio_home, R.id.act_main_quotes_works, R.id.act_main_comunity_publish, R.id.main_account_msg, R.id.act_main_my_account})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.act_main_radio_home:
                changePage(0);
                break;
            case R.id.act_main_quotes_works:
                changePage(1);
                break;
            case R.id.act_main_comunity_publish:
                changePage(2);
                break;
            case R.id.main_account_msg:
                changePage(3);
                break;
            case R.id.act_main_my_account:
                changePage(4);
                break;

                default:
                    break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (System.currentTimeMillis() - clicktime < 3000) {
                this.finish();
                MyUtil.getInstance().clear();
                Process.killProcess(Process.myPid());
                System.exit(0);
            } else {
                ToastUtil.showToast(getString(R.string.press_exit));
                clicktime = System.currentTimeMillis();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
}
